package file;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CSVParser {


    public static void main(String[] args) throws IOException {
        Stream<String> lines = Files.lines(Paths.get("D:/a.txt"));

        List<List<String>> collect = lines.map(CSVParser::parseRow).collect(Collectors.toList());
        for (List<String> list : collect) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                String val = list.get(i);
                switch (i) {
                    case 0:
                        sb.append(val);
                        break;
                    case 1:
                    case 2:
                        sb.append('\'').append(val).append('\'');
                        break;
                    case 3:
                        sb.append(Double.parseDouble(val));
                        break;
                    case 4:
                        sb.append(val);
                        break;
                }
                if (i < 4) {
                    sb.append('\t');
                }
            }
            System.out.println(sb);

        }

    }

    public static List<String> parseRow(String row) {
        char[] chars = row.toCharArray();
        Stack<Integer> expects = new Stack<>();
        StringBuilder value = new StringBuilder();
        // flag 2 不被""包裹,在遇到"标记为3,接着遇到“,”结束当前值,遇到与之前的""则转义为"",弹出标记的3,2被没被""包裹,下一个逗号结束当前值
        List<String> values = new ArrayList<>();
        for (int i = 0; i < chars.length; i++) {
            char cur = chars[i];
            if (expects.isEmpty()) {
                if (cur == '"') {
                    expects.push(1);
                } else if (cur != ',') {
                    expects.push(2);
                    value.append(cur);
                } else {
                    values.add(null);
                }
            } else {
                Integer flag = expects.peek();
                if (flag == 1) {
                    if (cur == '"') {
                        expects.push(3);
                    } else {
                        value.append(cur);
                    }
                } else if (flag == 2) {
                    if (cur == ',') {
                        expects.clear();
                        values.add(value.toString());
                        value.setLength(0);
                        if (i == chars.length - 1) {
                            values.add(null);
                        }
                    } else if (cur == '"') {
                        throw new IllegalArgumentException("错误格式! 引号需要被\"\"包裹且转义:" + row + ",在" + value + "之后");
                    } else {
                        value.append(cur);
                    }
                } else if (flag == 3) {
                    if (cur == ',') {
                        expects.clear();
                        values.add(value.toString());
                        value.setLength(0);
                        if (i == chars.length - 1) {
                            values.add(null);
                        }
                    } else if (cur == '"') {
                        value.append(cur);
                        expects.pop();
                    } else {
                        throw new IllegalArgumentException("错误格式! 引号缺少转义:" + row + ",在" + value + "之后");
                    }
                }
            }
        }
        if (!expects.isEmpty()) {
            Integer flag = expects.peek();
            if (flag == 2 || flag == 3) {
                values.add(value.toString());
            } else {
                throw new IllegalArgumentException("错误格式! 引号没有结束:" + row + ",在" + value + "之后");
            }
        }
        return values;
    }
}
